package com.qwer.service.accesscontrol.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.qwer.mapper.accesscontrol.AccGroupRepository;
import com.qwer.model.accesscontrol.AccGroupInfo;
import com.qwer.service.accesscontrol.AccGroupService;

@Service
public class AccGroupServiceImpl implements AccGroupService{
    
    @Autowired
    private AccGroupRepository repository;
    
    

    @Override
    public Page<AccGroupInfo> findList(Pageable pageable,AccGroupInfo model) {
        
        Specification<AccGroupInfo> spec = new Specification<AccGroupInfo>() {

            @Override
            public Predicate toPredicate(Root<AccGroupInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                
                List<Predicate> list = new ArrayList<>();
                if (StringUtils.isNotEmpty(model.getGroupName())) {
                    list.add(cb.like(root.get("groupName"),"%"+model.getGroupName()+"%"));
                }
                
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
            
        };   
        Page<AccGroupInfo> orderMasterPage = repository.findAll(spec,pageable);
        
        List<AccGroupInfo> orderDTOList = orderMasterPage.getContent();

        return new PageImpl<AccGroupInfo>(orderDTOList, pageable, orderMasterPage.getTotalElements());
    }


    @Override
    public void save(AccGroupInfo model) {
         repository.save(model);
    }

    @Override
    public void deleteById(Integer id) {
        
         repository.delete(id);;
    }


    @Override
    public AccGroupInfo findById(Integer id) {
        return repository.findOne(id);
    }


	@Override
	public List<AccGroupInfo> getAll() {
		// TODO Auto-generated method stub
		return repository.findAll();
	}

}
